from flask_restful import fields
from flask_jwt_extended import current_user

map_user_level = {
    0: '初来乍到',
    1: '乐于助人',
    2: '乐于奉献'
}

map_application_state = {
    0: '待处理',
    1: '被拒绝',
    2: '已接受',
    3: '已完成'
}

map_mission_state = {
    0: '可申请',
    1: '待完成',
    2: '已完成',
    3: '已删除'
}


user_profile_dto = {
    'id': fields.String,
    'username': fields.String,
    'phone': fields.String,
    'description': fields.String,
    'department': fields.String,
    'credit': fields.Float,
    'level': fields.String(attribute=lambda x: map_user_level[x.level]),
    'tags': fields.List(fields.String, attribute=lambda x: x.tags),
}


application_no_mission_dto = {
    'id': fields.String,
    'description': fields.String,
    'state': fields.String(attribute=lambda x: map_application_state[x.state]),
    'applier': fields.Nested(user_profile_dto),
    'created': fields.DateTime(dt_format='iso8601'),
    'updated': fields.DateTime(dt_format='iso8601')
}

mission_dto = {
    'id': fields.String,
    'type': fields.String,
    'title': fields.String,
    'description': fields.String,
    'people': fields.Integer,
    'credit_per_people': fields.Float,
    'deadline': fields.DateTime(dt_format='iso8601'),
    'picture': fields.String,
    'state': fields.String(attribute=lambda x: map_mission_state[x.state]),
    'owner': fields.Nested(user_profile_dto),
    'is_owner': fields.Boolean(attribute=lambda x: x.owner_id == current_user.id),
    'applications': fields.Nested(application_no_mission_dto),
    'created': fields.DateTime(dt_format='iso8601'),
    'updated': fields.DateTime(dt_format='iso8601')
}

mission_no_applications_dto = {
    'id': fields.String,
    'type': fields.String,
    'title': fields.String,
    'description': fields.String,
    'people': fields.Integer,
    'credit_per_people': fields.Float,
    'deadline': fields.DateTime(dt_format='iso8601'),
    'picture': fields.String,
    'state': fields.String(attribute=lambda x: map_mission_state[x.state]),
    'owner': fields.Nested(user_profile_dto),
    'is_owner': fields.Boolean(attribute=lambda x: x.owner_id == current_user.id),
    'created': fields.DateTime(dt_format='iso8601'),
    'updated': fields.DateTime(dt_format='iso8601')
}


application_dto = {
    'id': fields.String,
    'description': fields.String,
    'state': fields.String(attribute=lambda x: map_application_state[x.state]),
    'applier': fields.Nested(user_profile_dto),
    'mission': fields.Nested(mission_no_applications_dto),
    'created': fields.DateTime(dt_format='iso8601'),
    'updated': fields.DateTime(dt_format='iso8601')
}

user_profile_with_missions_dto = {
    'id': fields.String,
    'username': fields.String,
    'phone': fields.String,
    'description': fields.String,
    'department': fields.String,
    'missions': fields.Nested(mission_dto),
    'credit': fields.Float,
    'level': fields.String(attribute=lambda x: map_user_level[x.level]),
    'tags': fields.List(fields.String, attribute=lambda x: x.tags),
}
